package com.letu.core.context;

import cn.hutool.core.convert.Convert;
import com.alibaba.ttl.TransmittableThreadLocal;
import com.letu.annotation.base.LanguageTypeEnum;
import com.letu.annotation.base.SubjectTypeEnum;
import com.letu.core.utils.StrPool;
import com.letu.core.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 获取当前线程变量中的 用户id、用户昵称、租户编码、账号等信息
 *
 * @author ligang
 * @date 2017-12-13 16:52
 */
public final class ContextUtil {
    private ContextUtil() {
    }

    private static final ThreadLocal<Map<String, String>> THREAD_LOCAL = new TransmittableThreadLocal<>();

    public static void set(String key, Object value) {
        Map<String, String> map = getLocalMap();
        map.put(key, value == null ? StrPool.EMPTY : value.toString());
    }

    public static <T> T get(String key, Class<T> type) {
        Map<String, String> map = getLocalMap();
        return Convert.convert(type, map.get(key));
    }

    public static <T> T get(String key, Class<T> type, Object def) {
        Map<String, String> map = getLocalMap();
        return Convert.convert(type, map.getOrDefault(key, String.valueOf(def == null ? StrPool.EMPTY : def)));
    }

    public static String get(String key) {
        Map<String, String> map = getLocalMap();
        return map.getOrDefault(key, StrPool.EMPTY);
    }

    public static Map<String, String> getLocalMap() {
        Map<String, String> map = THREAD_LOCAL.get();
        if (map == null) {
            map = new ConcurrentHashMap<>(10);
            THREAD_LOCAL.set(map);
        }
        return map;
    }

    public static void setLocalMap(Map<String, String> localMap) {
        THREAD_LOCAL.set(localMap);
    }


    /**
     * 用户ID
     *
     * @return 用户ID
     */
    public static Long getUserId() {
        return get(ContextConstants.JWT_KEY_USER_ID, Long.class);
    }

    /**
     * 用户ID
     *
     * @param userId 用户ID
     */
    public static void setUserId(Long userId) {
        set(ContextConstants.JWT_KEY_USER_ID, userId);
    }


    /**
     * 用户姓名
     *
     * @return 用户姓名
     */
    public static String getName() {
        return get(ContextConstants.JWT_KEY_NAME, String.class);
    }

    /**
     * 用户姓名
     *
     * @param name 用户姓名
     */
    public static void setName(String name) {
        set(ContextConstants.JWT_KEY_NAME, name);
    }

    /**
     * 获取token
     *
     * @return token
     */
    public static String getToken() {
        return get(ContextConstants.BEARER_HEADER_KEY, String.class);
    }

    public static void setToken(String token) {
        set(ContextConstants.BEARER_HEADER_KEY, token);
    }



    public static void remove() {
        THREAD_LOCAL.remove();
    }



    public static String getTrace() {
        return get(ContextConstants.LOG_TRACE_ID, String.class, StrPool.EMPTY);
    }

    public static void setTrace(String val) {
        set(ContextConstants.LOG_TRACE_ID, val);
    }




    public static String getTerminalType() {
        return get(ContextConstants.TERMINAL_TYPE, String.class, StrPool.EMPTY);
    }

    public static void setTerminalType(String val) {
        set(ContextConstants.TERMINAL_TYPE, val);
    }




    public static void setIpAddr(String val) {
        set(ContextConstants.IPADDR, val);
    }

    public static String getIpAddr() {
        return get(ContextConstants.IPADDR, String.class, "");
    }



    public static void setXFeign(String val) {
        set(ContextConstants.FEIGN, val);
    }

    public static String getXFeign() {
        return get(ContextConstants.FEIGN, String.class, "");
    }

    /**
     * 是否超级管理员
     *
     * @return 用户姓名
     */
    public static Integer getIsSuperAdmin() {
        return get(ContextConstants.JWT_KEY_IS_SUPER_ADMIN, Integer.class);
    }

    /**
     * 是否业务管理员
     *
     * @param isSuperAdmin 是否管理员
     */
    public static void setIsSuperAdmin(Integer isSuperAdmin) {
        set(ContextConstants.JWT_KEY_IS_SUPER_ADMIN, isSuperAdmin);
    }

    /**
     * 网关 -token
     */
    public static void setGatewayToken(String val) {
        set(ContextConstants.GATEWAY_TOKEN, val);
    }

    public static String getGatewayToken() {
        return get(ContextConstants.GATEWAY_TOKEN, String.class, "");
    }


    /**
     * 网关 -创建时间
     */

    public static void setGatewayCreateTime(Long val) {
        set(ContextConstants.GATEWAY_CREATE_TIME, val);
    }

    public static Long getGatewayCreateTime() {
        return get(ContextConstants.GATEWAY_CREATE_TIME, Long.class, 0L);
    }


    /**
     * 网关 -请求地址
     */
    public static void setGatewayUrl(String val) {
        set(ContextConstants.GATEWAY_RQUEST_URL, val);
    }

    public static String getGatewayUrl() {
        return get(ContextConstants.GATEWAY_RQUEST_URL, String.class);
    }


    /**
     * 网关 -请求方法
     */
    public static void setGatewayMethod(String val) {
        set(ContextConstants.GATEWAY_RQUEST_METHOD, val);
    }

    public static String getGatewayMethod() {
        return get(ContextConstants.GATEWAY_RQUEST_METHOD, String.class);
    }

    /**
     * 网关 -请求参数
     */
    public static void setGatewayParams(String val) {
        set(ContextConstants.GATEWAY_RQUEST_PARAMS, val);
    }

    public static String getGatewayParams() {
        return get(ContextConstants.GATEWAY_RQUEST_PARAMS, String.class);
    }


    /**
     * 网关 -请求参数
     */
    public static void setTenantIdList(String val) {
        set(ContextConstants.JWT_KEY_TENANT_ID_LIST, val);
    }

    public static String getTenantIdList() {
        return get(ContextConstants.JWT_KEY_TENANT_ID_LIST, String.class);
    }


    /**
     * 网关 -请求参数
     */
    public static void setGameTag(String val) {
        set(ContextConstants.JWT_KEY_GAME_TAG, val);
    }

    public static String getGameTag() {
        return get(ContextConstants.JWT_KEY_GAME_TAG, String.class);
    }

    /**
     * 网关 -phoneid
     */
    public static void setPhoneId(String val) {
        set(ContextConstants.JWT_KEY_PHONE_ID, val);
    }

    public static String getPhoneId() {
        return get(ContextConstants.JWT_KEY_PHONE_ID, String.class);
    }

    /**
     * DB固定
     */
    public static void setDbNumber(Integer val) {
        set(ContextConstants.DB_NUMBER, val);
    }

    public static Integer getDbNumber() {
        return get(ContextConstants.DB_NUMBER, Integer.class);
    }

    /**
     * Table固定
     */
    public static void setTableNumber(Integer val) {
        set(ContextConstants.TABLE_NUMBER, val);
    }

    public static Integer getTableNumber() {
        return get(ContextConstants.TABLE_NUMBER, Integer.class);
    }


    /**
     * 地区
     */
    public static void setAreaCode(Integer val) {
        set(ContextConstants.JWT_KEY_USER_AREA_CODE, val);
    }

    public static Integer getAreaCode() {
        return get(ContextConstants.JWT_KEY_USER_AREA_CODE, Integer.class);
    }


    /**
     * 用户业务ID
     *
     * @return 用户ID
     */
    public static Long getUserBusinessId() {
        return get(ContextConstants.USER_BUSINESS_ID, Long.class);
    }

    /**
     * 用户业务ID
     *
     * @param userId 用户ID
     */
    public static void setUserBusinessId(Long userId) {
        set(ContextConstants.USER_BUSINESS_ID, userId);
    }



    /**
     * 是否要进行caffine缓存
     *
     * @return 用户ID
     */
    public static Integer getCaffineCacheIsOpen() {
        return get(ContextConstants.CAFEINE_CACHE, Integer.class);
    }

    /**
     * 是否要进行caffine缓存
     *
     * @param isOpen 用户ID
     */
    public static void setCaffineCacheIsOpen(Integer isOpen) {
        set(ContextConstants.CAFEINE_CACHE, isOpen);
    }

}
